<?php
class URI extends ArrayObject
{
	private static $instance = null;
	
	private $response = '';
	
	public function __construct() {
		$this->setURI();
		$this->setResponse();
	}
	
	public static function getInstance() {
		if (self::$instance == null) {
			self::$instance = new URI();
		}
		
		return self::$instance;
	}
	
	public function __toString() {
		return '/'.join('/', (array)$this);
	}
	
	public function getResponse() {
		return $this->response;
	}
	
	private function setURI() {
		if (isset($_SERVER['REQUEST_URI'])) {
			$uri = $_SERVER['REQUEST_URI'];
		} else {
			$uri = '';
		}
		
		while (strstr($uri, '//')) {
			$uri = str_replace('//', '/', $uri);
		}
		
		if (substr($uri, 0, 1) == '/') {
			$uri = substr($uri, 1);
		}
		
		if (substr($uri, -1) == '/') {
			$uri = substr($uri, 0, -1);
		}
		
		$this->exchangeArray(explode('/', $uri));
	}
	
	private function setResponse() {
		$keys = array_keys((array)$this);
		$last = end($keys);
		$this->response = pathinfo($this[$last], PATHINFO_EXTENSION);
		if (!empty($this->response)) {
			$this[$last] = str_replace('.'.$this->response, '', $this[$last]);
		}
	}
}
?>